﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OnLineExam.BLL;
using System.Data;
using OnLineExam.Model;
using NLog;
using HHG.Common;

namespace OnLineExam.Web
{
    public partial class StudentIndex : BasePage
    {
        private OnLineExam.Data.EfRepository<OnLineExam.Data.Paper> _paperRepository;
      

        protected void Page_Load(object sender, EventArgs e)
        {
            _paperRepository = new Data.EfRepository<Data.Paper>(_ctx);


            if (!Page.IsPostBack)
            {
                string userId = User.Identity.Name;
                string userName = UserManager.GetUserName(userId);
                lblName.Text = userName;


                //string sql = "select * FROM Paper where joinemps like '%" + User.Identity.Name + "%' and startExamTime <=getdate()  and   [Paper].EndExamTime >=getdate()";

                var query = _paperRepository.TableNoTracking.Where(s =>  s.startExamTime <= DateTime.Now
                  && s.EndExamTime >= DateTime.Now
                 ).ToList() ;



                ddlPaper.DataValueField = "PaperID";
                ddlPaper.DataTextField = "PaperName";
                ddlPaper.DataSource = query.Where(s=>s.joinemps.IsNullOrWhiteSpace()||(s.joinemps+"").Trim()=="*" || s.joinemps.Contains(userId));

                ddlPaper.DataBind();





            }
        }

        protected void startExam_Click(object sender, EventArgs e)
        {
            try
            {
                string userId = User.Identity.Name;
                var paperId = ddlPaper.SelectedValue.GetInt();

                if (Request.UserAgent.ToLower().Contains("linux"))
                {
                    //Response.Redirect("mobile/test.aspx?id=" + PaperID);
                }
                var paper = _paperRepository.GetById(paperId);

                int allwortextcount = paper.allowExamNum;//允许考试次数


                var checkInfo = Extensions.PaperExtension.CheckIsAllowExam(userId, paperId);
                if (checkInfo.Result == false)
                {
                    lblMessage.Text = checkInfo.Message;

                    return;
                }



                Session["PaperID"] = ddlPaper.SelectedValue;
                Session["PaperName"] = ddlPaper.SelectedItem.Text;
                //Session["userID"] = User.Identity.Name;
                //Session["examtime"] = Session["userID"].ToString();
                //Session["userName"] = labUser.Text;
                //Response.Write("<script>window.open('UserTest.aspx?paperId=" + ddlPaper.SelectedValue + "',null,'fullscreen=1')</script>");
                Response.Redirect("UserTest.aspx?PaperId=" + ddlPaper.SelectedValue + "");
                //Response.Write("<script language=javascript>window.close('StudentIndex.aspx');</script>");

            }
            catch (Exception ex)
            {

                logger.Error(ex);
                lblMessage.Text = ex.Message;


                return;
            }



        }

    }
}